package com.cpi.cornpp.common.files;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.common.files.dto.SAPComponentCornForecastDTO;
import com.cpi.cornpp.common.files.dto.SAPComponentDTO;
import com.cpi.cornpp.common.files.exception.SAPComponentFileException;
import com.cs.common.pool.exception.PoolException;
import com.cs.common.utils.NumberUtil;
/**
 * Componente que parsea el archivo Corn Forecast
 * @author Edgar Joao
 * @see SAPComponentFile
 */
public class SAPComponentCornForecast extends SAPComponentFile {
	private static final String CHAR_DELIMITER = "\t";//$NON-NLS-1$
	private static final int OFF_SET_INIT = 1;	
	private static final int OFF_SET_END = 1;
	private static final String SQL_INSERT_CORN_FORECAST = "INSERT INTO sap_sales_forecast ([customer] ,[material] ,[fiscal_year] ,[period] ,[contracted] ,[non_contracted]) VALUES (?, ?, ?, ?, ?, ?)";//$NON-NLS-1$
	public SAPComponentCornForecast(String argFileName) {
		super(argFileName);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insert(com.cpi.cornpp.common.files.dto.SAPComponentDTO)
	 */
	public void insert(SAPComponentDTO sapComponentDTO)
			throws SAPComponentFileException {
		SAPComponentCornForecastDTO sapComponentCornForecastDTO = null;
		PreparedStatement preparedStatement = null;		
		try{			
			sapComponentCornForecastDTO = (SAPComponentCornForecastDTO) sapComponentDTO;			
			preparedStatement = prepareSQL(SQL_INSERT_CORN_FORECAST, dsName);
			preparedStatement.setString(1, sapComponentCornForecastDTO.getCustomer());
			preparedStatement.setString(2, sapComponentCornForecastDTO.getMaterial());
			preparedStatement.setInt(3, sapComponentCornForecastDTO.getFiscalYear());
			preparedStatement.setInt(4, sapComponentCornForecastDTO.getPeriod());
			preparedStatement.setDouble(5, sapComponentCornForecastDTO.getContracted());
			preparedStatement.setDouble(6, sapComponentCornForecastDTO.getNonContracted());
			executeInsert(preparedStatement);
		}catch(PoolException poolException){
			SAPComponentFileException sapComponentFileException = null;
			poolException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}catch(SQLException sqlException){
			SAPComponentFileException sapComponentFileException = null;
			sqlException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (sqlException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}finally{
			try {
				closeConnection(preparedStatement.getConnection());
			} catch (SQLException sqlException) {}
		}	
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insertAll(java.util.List)
	 */
	public int insertAll(List list) throws SAPComponentFileException {
		int totalInserted = 0;
		Iterator iterator = null;
		SAPComponentCornForecastDTO sapComponentCornForecastDTO;
		try{
			iterator = list.iterator();
			while(iterator.hasNext()){
				sapComponentCornForecastDTO = (SAPComponentCornForecastDTO)iterator.next();
				try{
					insert(sapComponentCornForecastDTO);
					totalInserted++;
				}catch(SAPComponentFileException sapComponentFileException){
					System.out.println("Error DTO [" + sapComponentFileException.getMessage() + "] : " + sapComponentCornForecastDTO);
				}
			}
		} catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			exception.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (exception, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw sapComponentFileException;
		}
		return totalInserted;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#parse(java.util.List)
	 */
	public List parse(List list) throws SAPComponentFileException {
		List sapComponentCornForecastDTOs = null;
		Iterator iterator = null;
		String line = null;
		SAPComponentCornForecastDTO sapComponentCornForecastDTO = null;
		StringTokenizer stringTokenizer = null;
		String token = null;
		int totalReg = 0;		
		try{
			iterator = list.iterator();
			totalReg = list.size();
			sapComponentCornForecastDTOs = new ArrayList(0);			
			for( int mainIndex = 0; mainIndex < totalReg - OFF_SET_END ; mainIndex++ ){
				line = (String) iterator.next();
				if( mainIndex + 1 > OFF_SET_INIT ){
					stringTokenizer = new StringTokenizer(line, CHAR_DELIMITER, false);
					sapComponentCornForecastDTO = new SAPComponentCornForecastDTO();
					for(int index = 0;stringTokenizer.hasMoreTokens();index++){
						token = stringTokenizer.nextToken();
						switch(index){
							case 0:	sapComponentCornForecastDTO.setCustomer(token);
										break;
							case 1:	sapComponentCornForecastDTO.setMaterial(token);
										break;							
							case 2:	sapComponentCornForecastDTO.setFiscalYear(NumberUtil.parseInt(token));
										break;										
							case 3: sapComponentCornForecastDTO.setPeriod(NumberUtil.parseInt(token));
										break;			
							case 4:	sapComponentCornForecastDTO.setContracted(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY).replaceAll(" MT", StringUtils.EMPTY)));
										break;					
							case 5:	sapComponentCornForecastDTO.setNonContracted(NumberUtil.parseDouble(token.replaceAll("\"", StringUtils.EMPTY)));
										break;					
						}
					}
					sapComponentCornForecastDTOs.add(sapComponentCornForecastDTO);
				}
			}
		}catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			sapComponentFileException = new SAPComponentFileException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw sapComponentFileException;
		}
		return sapComponentCornForecastDTOs;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#size()
	 */
	public int size() throws SAPComponentFileException {
		int size = 0;
		List records = null;
		try{
			records = getContent();
			size = records.size() - OFF_SET_INIT-OFF_SET_END;
		}finally{
			records = null;
		}
		return size;
	}

}
